package com.example.tianyutong.qanda.ui.question;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.example.tianyutong.qanda.R;
import com.example.tianyutong.qanda.base.BaseActivity;
import com.example.tianyutong.qanda.base.BaseViewModel;
import com.example.tianyutong.qanda.bean.GankIoDataBean;
import com.example.tianyutong.qanda.bean.wanandroid.BaseResultBean;
import com.example.tianyutong.qanda.databinding.ActivityAddAnswerBinding;
import com.example.tianyutong.qanda.http.HttpClient;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @Date 2020/4/18 13:02
 * @Desc 添加回答
 */

public class AddAnswerActivity extends BaseActivity<BaseViewModel, ActivityAddAnswerBinding> implements View.OnClickListener {
    private long questionId;//问题id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_answer);
        setTitle("回答问题");
        questionId = getIntent().getLongExtra("_id", 0);

        bindingView.btnCommit.setOnClickListener(this);

        showContentView();

    }

    @Override
    public void onClick(View v) {
        String content = bindingView.tvContent.getText().toString();
        if (TextUtils.isEmpty(content.trim())) {
            Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
            return;
        }
        HttpClient.Builder.getQAndAServer().addAnswer(questionId,content)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResultBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(BaseResultBean bean) {
                        showContentView();
                        if (bean.getErrorCode() == 0) {
                            Toast.makeText(AddAnswerActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(AddAnswerActivity.this, bean.getErrorMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(AddAnswerActivity.this, "提交失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
